/*
 * COPYRIGHT. ShenZhen JiMi Technology Co., Ltd. 2017.
 * ALL RIGHTS RESERVED.
 *
 * No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
 * on any form or by any means, electronic, mechanical, photocopying, recording, 
 * or otherwise, without the prior written permission of ShenZhen JiMi Network Technology Co., Ltd.
 *
 * Amendment History:
 * 
 * Date                   By              Description
 * -------------------    -----------     -------------------------------------------
 * 2017年6月23日    Li.Shangzhi         Create the class
 * http://www.jimilab.com/
 */

package com.jimi;

import java.util.Iterator;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.logging.LoggingApplicationListener;
import org.springframework.context.ApplicationListener;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ResourceBundleMessageSource;

import com.jimi.framework.context.SpringContextHolder;

/**
 * @FileName Provider.java
 * @Description: 程序入口
 *
 * @Date 2017年6月23日 下午4:58:13
 * @author Li.Shangzhi
 * @version 1.0
 */
@SpringBootApplication
@ComponentScan(basePackages = "com.jimi")
@ImportResource({ "classpath:applicationContext-dubbo.xml" })
public class ProviderApplication  implements CommandLineRunner {

	private static final Logger logger = LoggerFactory.getLogger(ProviderApplication.class);

	private static final String EXIT_CODE = "exitcode";
	
	@Override
	public void run(String... args) throws Exception {
		if (args.length > 0 && EXIT_CODE.equals(args[0])) {
			throw new ExitException();
		}
	}

	public static void main(String[] args) {
		//去除spring-boot 对logback初始化
		SpringApplicationBuilder builder = new SpringApplicationBuilder(ProviderApplication.class);
		Set<ApplicationListener<?>> listeners = builder.application().getListeners();
		for (Iterator<ApplicationListener<?>> it = listeners.iterator(); it.hasNext();) {
		    ApplicationListener<?> listener = it.next();
		    if (listener instanceof LoggingApplicationListener) {
		        it.remove();
		    }
		}
		builder.application().setListeners(listeners);
		builder.run(args);
		
		logger.info("====================启动成功!====================");
	}
	
	@Bean()
	public BeanNameAutoProxyCreator adapter() {
		BeanNameAutoProxyCreator adapter = new BeanNameAutoProxyCreator();
		adapter.setProxyTargetClass(true);
		adapter.setBeanNames("*ApiController*");
		adapter.setInterceptorNames("ovalGuardInterceptor");
		return adapter;
	}

	@Bean
	public MessageSource messageSource() {
		final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		messageSource.setBasename("i18n/messages");
		messageSource.setFallbackToSystemLocale(false);
		messageSource.setCacheSeconds(0);
		messageSource.setDefaultEncoding("UTF-8");
		return messageSource;
	}

	@Bean
	public SpringContextHolder springContextHolder() {
		return new SpringContextHolder();
	}

	class ExitException extends RuntimeException implements ExitCodeGenerator {
		private static final long serialVersionUID = 1L;

		@Override
		public int getExitCode() {
			return 10;
		}
	}
}
